package controller.business.observerAssess;

import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import itext.ItextFont;
import util.DateUtil;
import wt.entity.business.BusinessInfo;
import wt.entity.business.ObserverAssess;

import java.io.IOException;
import java.io.OutputStream;

public class ObserverAssessDoc {
    public static void createDoc(OutputStream outputStream, java.util.List<ObserverAssess> observerAssessList , BusinessInfo businessInfo) throws DocumentException, IOException {
        //设置纸张大小
        Document document = new Document(PageSize.A3);
        //建立一个书写器，与document对象关联
        RtfWriter2.getInstance(document, outputStream);
        document.open();





        Paragraph paragraph = new Paragraph("现场审查工作评价表（观察员）", ItextFont.fangZhengDoc18);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        paragraph.setSpacingBefore(30f);
        document.add(paragraph);

        
        //设置Table表格,创建一个7列的表格
        Table table = new Table(7);
        float width[] = {1, 1.1F, 1, 1, 1, 1, 1};//设置每列宽度比例
        table.setWidths(width);
        table.setWidth(100);//占页面宽度比例
        table.setAlignment(Element.ALIGN_CENTER);//居中
        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
        table.setAutoFillEmptyCells(true);//自动填满
        table.setBorderWidth(1);//边框宽度
        table.setPadding(15F);

        Cell[] cell = new Cell[6];
        cell[0] = new Cell(new Phrase("企业名称", ItextFont.fangSongNormalDoc10));
        cell[1] = new Cell(new Phrase( businessInfo.getCompanyName(), ItextFont.fangSongNormalDoc10));
        cell[1].setColspan(2);
        cell[2] = new Cell(new Phrase("产品名称", ItextFont.fangSongNormalDoc10));
        cell[3] = new Cell(new Phrase( businessInfo.getProductName(), ItextFont.fangSongNormalDoc10));
        cell[4] = new Cell(new Phrase("核查时间", ItextFont.fangSongNormalDoc10));
        cell[5] = new Cell(new Phrase(DateUtil.date2String(businessInfo.getEndTime(),"yyyy-MM-dd"), ItextFont.fangSongNormalDoc10));
        addTable(cell,table);

        cell = new Cell[7];
        for (int i = 0;i  <observerAssessList.size() ; i++) {
            ObserverAssess temp=observerAssessList.get(i);
            if ("leader".equals(temp.getInspectorInfo().getExpertRole())) {
                cell[0] = new Cell(new Phrase("审查组长姓名", ItextFont.fangSongBoldDoc10));
                cell[1] = new Cell(new Phrase("审查组长证书编号", ItextFont.fangSongBoldDoc10));
                cell[2] = new Cell(new Phrase("组织协调能力", ItextFont.fangSongBoldDoc10));
                cell[3] = new Cell(new Phrase("审查业务能力", ItextFont.fangSongBoldDoc10));
                cell[4] = new Cell(new Phrase("公平公正性", ItextFont.fangSongBoldDoc10));
                cell[5] = new Cell(new Phrase("廉洁自律", ItextFont.fangSongBoldDoc10));
                cell[6] = new Cell(new Phrase("综合评价", ItextFont.fangSongBoldDoc10));
                addTable(cell,table);
                cell[0] = new Cell(new Phrase(temp.getInspectorInfo().getExpertName(), ItextFont.fangSongNormalDoc10));
                cell[1] = new Cell(new Phrase(temp.getInspectorInfo().getCertificateNum(), ItextFont.fangSongNormalDoc10));
                cell[2] = new Cell(createCellText(temp.getCooperation()));
                cell[3] = new Cell(createCellText(temp.getBusiness()));
                cell[4] = new Cell(createCellText(temp.getFairness()));
                cell[5] = new Cell(createCellText(temp.getHonest()));
                cell[6] = new Cell(createCellText(temp.getComprehensive()));
                addTable(cell,table);
                break;
            }
        }


        boolean flag=false;
        for (int i = 0;i  <observerAssessList.size() ; i++) {
            ObserverAssess temp=observerAssessList.get(i);
            if ("crew".equals(temp.getInspectorInfo().getExpertRole()) && !"C".equals(temp.getInspectorInfo().getCertificateGrade())){
                if (flag==false){
                    cell[0] = new Cell(new Phrase("审查组成员姓名", ItextFont.fangSongBoldDoc10));
                    cell[1] = new Cell(new Phrase("审查员证书编号", ItextFont.fangSongBoldDoc10));
                    cell[2] = new Cell(new Phrase("协调配合能力", ItextFont.fangSongBoldDoc10));
                    cell[3] = new Cell(new Phrase("审查业务能力", ItextFont.fangSongBoldDoc10));
                    cell[4] = new Cell(new Phrase("公平公正性", ItextFont.fangSongBoldDoc10));
                    cell[5] = new Cell(new Phrase("廉洁自律", ItextFont.fangSongBoldDoc10));
                    cell[6] = new Cell(new Phrase("综合评价", ItextFont.fangSongBoldDoc10));
                    addTable(cell,table);
                    flag=true;
                }


                cell[0] = new Cell(new Phrase(temp.getInspectorInfo().getExpertName(), ItextFont.fangSongNormalDoc10));
                cell[1] = new Cell(new Phrase(temp.getInspectorInfo().getCertificateNum(), ItextFont.fangSongNormalDoc10));
                cell[2] = new Cell(createCellText(temp.getCooperation()));
                cell[3] = new Cell(createCellText(temp.getBusiness()));
                cell[4] = new Cell(createCellText(temp.getFairness()));
                cell[5] = new Cell(createCellText(temp.getHonest()));
                cell[6] = new Cell(createCellText(temp.getComprehensive()));
                
                
                addTable(cell,table);
            }
        }


        flag=false;
        for (int i = 0;i  <observerAssessList.size() ; i++) {
            ObserverAssess temp=observerAssessList.get(i);
            if ("crew".equals(temp.getInspectorInfo().getExpertRole()) && "C".equals(temp.getInspectorInfo().getCertificateGrade())){
                if (flag==false){
                    cell[0] = new Cell(new Phrase("实习审查员姓名", ItextFont.fangSongBoldDoc10));
                    cell[1] = new Cell(new Phrase("实习审查员证书编号", ItextFont.fangSongBoldDoc10));
                    cell[2] = new Cell(new Phrase("协调配合能力", ItextFont.fangSongBoldDoc10));
                    cell[3] = new Cell(new Phrase("审查业务能力", ItextFont.fangSongBoldDoc10));
                    cell[4] = new Cell(new Phrase("公平公正性", ItextFont.fangSongBoldDoc10));
                    cell[5] = new Cell(new Phrase("廉洁自律", ItextFont.fangSongBoldDoc10));
                    cell[6] = new Cell(new Phrase("综合评价", ItextFont.fangSongBoldDoc10));
                    addTable(cell,table);
                    flag=true;
                }
                cell[0] = new Cell(new Phrase(temp.getInspectorInfo().getExpertName(), ItextFont.fangSongNormalDoc10));
                cell[1] = new Cell(new Phrase(temp.getInspectorInfo().getCertificateNum(), ItextFont.fangSongNormalDoc10));
                cell[2] = new Cell(createCellText(temp.getCooperation()));
                cell[3] = new Cell(createCellText(temp.getBusiness()));
                cell[4] = new Cell(createCellText(temp.getFairness()));
                cell[5] = new Cell(createCellText(temp.getHonest()));
                cell[6] = new Cell(createCellText(temp.getComprehensive()));
                addTable(cell,table);
            }
        }

        paragraph = new Paragraph();
        paragraph.add(table);
        document.add(paragraph);
        paragraph = new Paragraph("观察员（签字）：                             年       月       日\n" +
                "填表说明：1、本表由区县市场监管局观察员填写。 2、“综合评价”的判定原则为：有2项以上(含2项)为“一般”的，综合评价为“一般”；有1项以上(含 1项)为“差”的，综合评价为“差”；其他情况综合评价为“好”。\n" , ItextFont.fangSongNormalDoc10);
        document.add(paragraph);

        document.close();

    }


    public static void  addTable(Cell[] cell ,Table table){
        for (int i = 0; i < cell.length; i++) {
            cell[i].setHorizontalAlignment(Element.ALIGN_CENTER);//居中显示
            cell[i].setVerticalAlignment(Element.ALIGN_MIDDLE);//纵向居中显示
            table.addCell(cell[i]);
        }
    }

    public static Phrase   createCellText(int i){
        Phrase phrase=null;
        if (0==i){
            phrase=new Paragraph("好□ 一般□ 差☑", ItextFont.fangSongNormalDoc10);
        }else if (1==i){
            phrase=new Paragraph("好□ 一般☑ 差□", ItextFont.fangSongNormalDoc10);
        }else {
            phrase=new Paragraph("好☑ 一般□ 差□", ItextFont.fangSongNormalDoc10);
        }
        return phrase;
    }


}

